<template>
  <div class="login_container" @keydown.enter="submit">
    <div class="login_box">
      <!-- logo区域 -->
      <div class="login_logo">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单区域 -->
      <div class="login_form" @keydown.enter="submit">
        <!-- 用户名 -->
        <el-form :rules="rules" :model="loginForm" ref="loginFormRef">
          <el-form-item prop="username">
            <el-input
              prefix-icon="el-icon-user-solid"
              placeholder="请输入用户名"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              v-model="loginForm.password"
              type="password"
              ref="passwordForm"
            ></el-input>
            <p class="eye el-icon-view" @click="passwordVisable"></p>
          </el-form-item>

          <el-form-item class="btn">
            <el-button type="primary" @click="submit">登录</el-button>
            <el-button @click="resetLoginForm()">清空</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
    <script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 5,
            message: '长度需要在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            message: '长度大于6个字符且不能为全数字',
            trigger: 'blur'
          }
        ]
      },
      passwordFlag: true
    }
  },
  methods: {
    // 密码的显示与隐藏
    passwordVisable() {
      if (this.passwordFlag) {
        this.$refs.passwordForm.type = 'text'
      } else {
        this.$refs.passwordForm.type = 'password'
      }
      this.passwordFlag = !this.passwordFlag
    },
    // 重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 提交表单
    submit() {
      // element-ui的方法，回调参数valid为true即为通过上面的rules，反之没有通过
      this.$refs.loginFormRef.validate(valid => {
        if (valid) {
          //执行登录操作
          this.$http.post('login', this.loginForm).then(res => {
            if (res.data.meta.status != 200) this.$message.error('用户不存在')
            // console.log(res.data);
            this.$message.success('登录成功')
            //保存token到localstorage中
            window.localStorage.setItem('token', res.data.data.token)
            this.transfer()
          })
        }
      })
    },
    transfer() {
      if (window.localStorage.getItem('token')) {
        this.$router.push('/home')
      }
    },
    beforeMount() {
      this.transfer()
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #ccc;
}
.login_box {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
}
.login_logo {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 110px;
  height: 110px;
  padding: 10px;
  border: 1px solid #ccc;
  box-shadow: 0 0 10px #ddd;
  border-radius: 50%;
  background-color: #fff;
}
.login_logo img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.login_form {
  position: absolute;
  bottom: 0px;
  padding: 0 30px;
  width: 100%;
  box-sizing: border-box;
}
.login_form .btn {
  display: flex;
  justify-content: flex-end;
}
.password_form {
  position: relative;
}
.eye {
  position: absolute;
  right: 10px;
}
</style>
